﻿package  
{
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.*;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author ...
	 */
	public class wheel 
	{
		
		public var m_pos:cartesian;
		public var m_radius:Number;
		public var m_angle:Number = 0;
		public var m_wheelBody:b2Body;
		
		public function wheel(x:Number, y:Number, radius:Number, world:b2World) 
		{
			
			m_pos = new cartesian(x, y);
			m_radius = radius;
			
			var wheelShapeDef:b2CircleDef = new b2CircleDef();
			wheelShapeDef.radius = radius / main.m_physScale;
			wheelShapeDef.isSensor = false;
			wheelShapeDef.density = 2.0;
			wheelShapeDef.friction = 6.0;
			wheelShapeDef.restitution = 0.0;
			
			var wheelBodyDef:b2BodyDef = new b2BodyDef();
			wheelBodyDef.position.Set(x / main.m_physScale, y / main.m_physScale);
			wheelBodyDef.userData = this;
			
			m_wheelBody = world.CreateBody(wheelBodyDef);
			m_wheelBody.CreateShape(wheelShapeDef);
			m_wheelBody.SetMassFromShapes();

			
			
		}
		
		
		public function update():void
		{
			// Convert Physics Coord System to "Real" Coord System
			m_pos.x = m_wheelBody.GetPosition().x * main.m_physScale;
			m_pos.y = m_wheelBody.GetPosition().y * main.m_physScale;
			m_angle = (m_wheelBody.GetAngle() * (180 / Math.PI)) % 360;
			
			
			

		}
		
		public function draw(vSprite:Sprite, cam:camera):void
		{
			var r:int = m_radius * cam.zoom;
			var kx:Array = new Array(1.0, .924, .707, .383, 0, -.383, -.707, -.924, -1.0, -.924, -.707, -.383, 0, .383, .707, .924, 1.0); 
			var ky:Array = new Array(0, .383, .707, .924, 1.0, .924, .707, .383, 0, -.383, -.707, -.924, -1.0, -.924, -.707, -.383, 0);
			
			vSprite.graphics.lineStyle(1, 255);
			vSprite.graphics.moveTo(m_pos.toScreen(cam).x + r, m_pos.toScreen(cam).y);
			for (var i:int = 0; i < 17; i++)
			{
				vSprite.graphics.lineTo(m_pos.toScreen(cam).x + r * kx[i], m_pos.toScreen(cam).y + r * ky[i]);
			} 
			
			vSprite.graphics.lineStyle(1, 11184810);
			vSprite.graphics.moveTo(m_pos.toScreen(cam).x - r * Math.cos(m_angle * (Math.PI / 180)), m_pos.toScreen(cam).y - r * Math.sin(m_angle * (Math.PI / 180)));
			vSprite.graphics.lineTo(m_pos.toScreen(cam).x + r * Math.cos(m_angle * (Math.PI / 180)), m_pos.toScreen(cam).y + r * Math.sin(m_angle* (Math.PI / 180)));
		}
		
	}
	
}